#encoding=utf-8
import sys
import PyQt5.QtWidgets as qw
from PyQt5.QtCore import QThread, QTimer
import ui_uart_tool
import threading
from serial_thread import Serial_Thread_Function
from PyQt5.QtSerialPort import QSerialPortInfo

class Serial_Form(qw.QMainWindow):
    def __init__(self):
        super().__init__()
        self.ui = ui_uart_tool.Ui_MainWindow()
        self.ui.setupUi(self)
        print("主线程id:",threading.current_thread().ident)
        self.Serial_Thread_Inst = QThread()
        self.Serial_Thread_Function_Inst = Serial_Thread_Function()
        self.Serial_Thread_Function_Inst.moveToThread(self.Serial_Thread_Inst)
        self.Serial_Thread_Function_Inst.signal_start_function.connect(self.Serial_Thread_Function_Inst.Serial_Init_Function)
        self.Serial_Thread_Inst.start()
        self.Serial_Thread_Function_Inst.signal_start_function.emit()
        
        self.port_name = []
        self.time_scan = QTimer()
        self.time_scan.timeout.connect(self.Time_Out)
        self.time_scan.start(1000)
        
    def Time_Out(self):
        print('定时时间到')
        avaPort = QSerialPortInfo.availablePorts()
        newPort = []
        for port in avaPort:
            newPort.append(port.portName())
            
        if len(self.port_name)!=len(newPort):
            print(newPort)
            self.ui.comboBox_uart.clear()
            self.ui.comboBox_uart.addItems(newPort)
        
    def Interface_Init(self):
        pass
        
if __name__ == "__main__":
    app = qw.QApplication(sys.argv)
    w = Serial_Form()
    w.show()
    sys.exit(app.exec_())